/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.input.*
import com.almasb.fxgl.scene.*
import com.almasb.fxgl.trade.*
import javafx.scene.input.*

/**
 * This is an example of a minimalistic FXGL game application.
 *
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class TradeSample : GameApplication() {
	private lateinit var tradeSubScene: TradeSubScene
	
	override fun initSettings(settings: GameSettings) {
		settings.width = 1066
		settings.height = 600
	}
	
	override fun initGame() {
		tradeSubScene = TradeSubScene()
		
		onKeyDown(KeyCode.F, "f") { getGameController().pushSubScene(tradeSubScene) }
	}
	
	@Suppress("UNCHECKED_CAST")
	class TradeSubScene : SubScene() {
		init {
			
			val item1 = Entity()
			val item2 = Entity()
			val item3 = Entity()
			
			val playerShop = Shop(1000, listOf(
				TradeItem(item1, "10mm Pistol", "Item Description", 30, 65, 3),
				TradeItem(item2, "Longsword", "Item Description", 30, 35, 1)
			))
			
			val npcShop = Shop(2000, listOf(
				TradeItem(item3, "10mm Pistol", "Item Description", 30, 165, 3)
			))
			
			val playerView = ShopView(playerShop, 400, 450)
			val npcView = ShopView(npcShop, 400, 450)
			
			val view = TradeView(playerView, npcView)
			
			contentRoot.children.addAll(view)
			contentRoot.translateX = 100.0
			contentRoot.translateY = 50.0
			
			this.input.addAction(object : UserAction("Close") {
				override fun onActionBegin() {
					getGameController().popSubScene()
				}
			}, KeyCode.F)
			
			this.input.addAction(object : UserAction("Trade") {
				override fun onActionBegin() {
					val tab = view.tabPane.selectionModel.selectedItem
					
					val shopView = tab.content as ShopView<Entity>
					
					val item = shopView.listView.selectionModel.selectedItem as TradeItem<Entity>
					
					println("Item is : $item")
					
					if(shopView === playerView) {
						npcView.shop.buyFrom(playerShop, item, 1)
					} else {
						playerShop.buyFrom(npcShop, item, 1)
					}
					
					npcView.listView.refresh()
					playerView.listView.refresh()
				}
			}, KeyCode.S)
		}
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(TradeSample::class.java, args)
}
